package com.haidechizi.doublecache.aop;

import com.haidechizi.doublecache.annotation.DoubleCache;
import com.haidechizi.doublecache.process.AopProcessContext;
import com.haidechizi.doublecache.process.ProcessContext;
import com.haidechizi.doublecache.process.ProcessHolder;
import com.haidechizi.doublecache.process.ProcessService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * 双重缓存切面
 *
 * @author haidechizi
 */
@Aspect
public class DoubleCacheAspect {

    @Pointcut("@annotation(com.haidechizi.doublecache.annotation.DoubleCache)")
    public void cacheAspect() {

    }

    @Around("cacheAspect()")
    public Object doAround(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        DoubleCache doubleCache = method.getAnnotation(DoubleCache.class);

        ProcessService processService = ProcessHolder.getProcessService(doubleCache.type().getCode());
        ProcessContext processContext = new AopProcessContext(point);
        processService.process(processContext);
        return processContext.getValue();
    }
}
